package org.algorithm.biubiu.wheel_1.数组.三月.第四周;

/**
 * @author zhx
 * @date 2022-03-21 星期一18:25
 *
 * url: https://leetcode-cn.com/problems/trapping-rain-water/
 */
public class 接雨水 {


    public static void main(String[] args) {
        int[] height = {0,1,0,2,1,0,1,3,2,1,2,1};
        trap(height);
    }

    public static int trap(int[] height) {
        if(height==null||height.length==0) return -1;
        int left = 0;
        int right = height.length-1;
        int res = 0;
        int leftHeight = height[0];
        int rightHeight = height[height.length-1];
        while(left<right){
            //移动短板，
            if(leftHeight<=rightHeight){
                //312  递减&&递增 才会有存水
                if(height[left+1]<leftHeight){
                    res+=leftHeight-height[left+1];
                }else{
                    leftHeight = height[left+1];
                }
                left++;
            }else{
                if(height[right-1]<rightHeight){
                    res+=rightHeight-height[right-1];
                }else{
                    rightHeight = height[right-1];
                }
                right--;

            }
        }
        return res;
    }

}
